package tech.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import tech.aistar.entity.UserInfo;
import tech.aistar.entity.UserLogin;
import tech.aistar.entity.UserRelation;
import tech.aistar.service.IUserInfoService;
import tech.aistar.service.IUserLoginService;
import tech.aistar.service.IUserRelationService;

@Controller
@RequestMapping("/add")
public class AddUserContriller {
    @Autowired
    private IUserLoginService userLoginService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IUserRelationService userRelationService;

    @PostMapping("/phone")
    public String testPhone(UserInfo userInfo){
        if(userInfo.getuPhone().trim().length()<11){
            userInfo.setuPhone(null);
        }
        System.out.println(userInfo.getuPhone());
        userInfoService.addPhone(userInfo.getuPhone(),userInfo.getId());
        return "redirect:/info/agent";
    }

    @PostMapping("/email")
    public String testEmail(UserInfo userInfo){
        userInfoService.addEmail(userInfo.getuEmail(),userInfo.getId());
        return "redirect:/info/agent";
    }

    @PostMapping("/position")
    public String testPos(UserLogin userLogin){
        userLoginService.addCard(userLogin.getName(),userLogin.getNumder(),userLogin.getUserId());
        return "redirect:/info/agent";
    }

    @PostMapping("/focu/{id}/{userByid}")
    public String addFocu(@PathVariable("id")Integer id, @PathVariable("userByid")Integer userByid){
        //先找他俩之前是否有关系
        Long reId = userRelationService.findShip(id,userByid,1);
        if(null==reId){
            //之前无 加
            UserRelation u = new UserRelation(null,id,null,userByid,0);
            userRelationService.addShip(u);
        }else{
            //改
            userRelationService.chanageShip(id,userByid);
        }
       System.out.println("add----------------"+id+userByid);
        //重新后台加载该关注页面
        return "forward:/info/focuson";
    }
}
